package com.woniu.teachmall04.controller;

import com.woniu.teachmall04.entity.Admin;
import com.woniu.teachmall04.service.AdminService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    // 管理员登录页面
    @GetMapping("/login")
    public String loginPage() {
        return "admin/login";
    }

    // 管理员登录处理
    @PostMapping("/login")
    public String login(@RequestParam String account,
                        @RequestParam String password,
                        HttpSession session,
                        HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        Admin admin = adminService.login(account, password, ip);
        if(admin == null) {
            return "redirect:/admin/login?error=1";
        }
        session.setAttribute("admin", admin);
        return "redirect:/admin/dashboard";
    }

    // 管理员仪表盘
    @GetMapping("/dashboard")
    public String dashboard(Model model, HttpSession session) {
        Admin admin = (Admin) session.getAttribute("admin");
        if(admin == null) {
            return "redirect:/admin/login";
        }
        model.addAttribute("admin", admin);
        return "admin/dashboard";
    }

    // 管理员列表
    @GetMapping("/list")
    public String adminList(Model model, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute("admin");
        if(currentAdmin == null) {
            return "redirect:/admin/login";
        }

        List<Admin> admins = adminService.getAllAdmins();
        model.addAttribute("admins", admins);
        return "admin/list";
    }

    // 添加管理员页面
    @GetMapping("/add")
    public String addAdminPage(HttpSession session) {
        if(session.getAttribute("admin") == null) {
            return "redirect:/admin/login";
        }
        return "admin/add";
    }

    // 添加管理员处理
    @PostMapping("/add")
    public String addAdmin(Admin admin, HttpSession session) {
        if(session.getAttribute("admin") == null) {
            return "redirect:/admin/login";
        }

        if(!adminService.addAdmin(admin)) {
            return "redirect:/admin/add?error=1";
        }
        return "redirect:/admin/list";
    }

    // 编辑管理员页面
    @GetMapping("/edit/{id}")
    public String editAdminPage(@PathVariable Integer id, Model model, HttpSession session) {
        if(session.getAttribute("admin") == null) {
            return "redirect:/admin/login";
        }

        Admin admin = adminService.getAllAdmins().stream()
                .filter(a -> a.getId().equals(id))
                .findFirst()
                .orElse(null);

        if(admin == null) {
            return "redirect:/admin/list";
        }

        model.addAttribute("admin", admin);
        return "admin/edit";
    }

    // 更新管理员信息
    @PostMapping("/update")
    public String updateAdmin(Admin admin, HttpSession session) {
        if(session.getAttribute("admin") == null) {
            return "redirect:/admin/login";
        }

        if(!adminService.updateAdmin(admin)) {
            return "redirect:/admin/edit/" + admin.getId() + "?error=1";
        }
        return "redirect:/admin/list";
    }

    // 禁用/启用管理员
    @PostMapping("/toggle-status/{id}")
    public String toggleAdminStatus(@PathVariable Integer id,
                                    @RequestParam Integer status,
                                    HttpSession session) {
        if(session.getAttribute("admin") == null) {
            return "redirect:/admin/login";
        }

        adminService.toggleAdminStatus(id, status);
        return "redirect:/admin/list";
    }

    // 删除管理员
    @PostMapping("/delete/{id}")
    public String deleteAdmin(@PathVariable Integer id, HttpSession session) {
        if(session.getAttribute("admin") == null) {
            return "redirect:/admin/login";
        }

        adminService.deleteAdmin(id);
        return "redirect:/admin/list";
    }

    // 退出登录
    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "redirect:/admin/login";
    }
}